/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.mathml_canonicalizator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Richard
 */
public class MrootTransformer implements Transformer {
    
    private Node root;
    private Node msqrt = null;
    // private static final String configFile = "src/mfrac.properties";

    public MrootTransformer(String tagName, Map<String,String> attrs,
            Map<String, List<String>> properties) {
        
        List<String> attr_keep = properties.get("attr_keep") != null ?
                properties.get("attr_keep") : new ArrayList<String>();
        
        // removes all attributes that should be removed
        Util.removeAtrributes(attrs, attr_keep);
        
        root = new NodeImpl(tagName, attrs);
    }

    @Override
    public void addChild(Node n) {
        if (root.getChildren() != null && root.getChildren().size() > 0 &&
                n.getTextContent().trim().equals("2")) {
            
            // in this case mroot is in fact just msqrt
            
            msqrt = new NodeImpl("msqrt", new HashMap<String, String>());
            msqrt.addChild(root.getChildren().get(0));
            if (msqrt.getChildren().get(0).getTagName().equals("mrow")) {
                msqrt.getChildren().get(0).setShadow(true);
            }
            
        } else {
            root.addChild(n);
        }
    }

    @Override
    public void setTextContent(String s) {
    }

    @Override
    public Node transform() {
        return msqrt == null ? root : msqrt;
    }
}
